<template>
  <Teleport to="body">
    <Transition name="preview-fade">
      <div v-if="visible" class="image-preview-overlay">
        <!-- 顶部工具栏 -->
        <div class="preview-header">
          <div class="header-left">
            <span class="preview-counter">{{ currentIndex + 1 }} / {{ images.length }}</span>
          </div>
          <div class="header-right">
            <button class="toolbar-btn" @click.stop="handleZoomOut" title="缩小">
              <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                <circle cx="11" cy="11" r="8"></circle>
                <line x1="8" y1="11" x2="14" y2="11"></line>
                <line x1="21" y1="21" x2="16.65" y2="16.65"></line>
              </svg>
            </button>
            <button class="toolbar-btn" @click.stop="handleZoomIn" title="放大">
              <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                <circle cx="11" cy="11" r="8"></circle>
                <line x1="11" y1="8" x2="11" y2="14"></line>
                <line x1="8" y1="11" x2="14" y2="11"></line>
                <line x1="21" y1="21" x2="16.65" y2="16.65"></line>
              </svg>
            </button>
            <button class="toolbar-btn" @click.stop="handleResetZoom" title="1:1">
              <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                <rect x="3" y="3" width="18" height="18" rx="2" ry="2"></rect>
              </svg>
            </button>
            <button class="toolbar-btn" @click.stop="handleRotateLeft" title="向左旋转">
              <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                <polyline points="1 4 1 10 7 10"></polyline>
                <path d="M3.51 15a9 9 0 1 0 2.13-9.36L1 10"></path>
              </svg>
            </button>
            <button class="toolbar-btn" @click.stop="handleRotateRight" title="向右旋转">
              <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                <polyline points="23 4 23 10 17 10"></polyline>
                <path d="M20.49 15a9 9 0 1 1-2.12-9.36L23 10"></path>
              </svg>
            </button>
            <div class="toolbar-divider"></div>
            <button class="toolbar-btn close-btn" @click.stop="handleClose" title="关闭">
              <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                <line x1="18" y1="6" x2="6" y2="18"></line>
                <line x1="6" y1="6" x2="18" y2="18"></line>
              </svg>
            </button>
          </div>
        </div>

        <!-- 图片内容区域 -->
        <div class="preview-content" @click="handleClose">
          <div 
            class="image-container" 
            @click.stop
            :style="{
              transform: `scale(${scale}) rotate(${rotation}deg)`,
              transition: isTransitioning ? 'transform 0.3s ease' : 'none'
            }"
          >
            <img 
              :src="currentImage" 
              :alt="currentAlt" 
              class="preview-image"
              @load="handleImageLoad"
            />
          </div>
        </div>

        <!-- 左右切换按钮 -->
        <button 
          v-if="images.length > 1"
          class="nav-btn nav-btn-prev" 
          @click.stop="handlePrev" 
          :disabled="currentIndex === 0"
        >
          <svg width="32" height="32" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
            <polyline points="15 18 9 12 15 6"></polyline>
          </svg>
        </button>
        <button 
          v-if="images.length > 1"
          class="nav-btn nav-btn-next" 
          @click.stop="handleNext" 
          :disabled="currentIndex === images.length - 1"
        >
          <svg width="32" height="32" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
            <polyline points="9 18 15 12 9 6"></polyline>
          </svg>
        </button>
      </div>
    </Transition>
  </Teleport>
</template>

<script setup lang="ts">
import { ref, computed, watch, onMounted, onUnmounted } from 'vue'

interface ImageInfo {
  src: string
  alt?: string
}

const props = defineProps<{
  visible: boolean
  images: ImageInfo[]
  current: number
}>()

const emit = defineEmits<{
  'update:visible': [value: boolean]
  'update:current': [value: number]
}>()

const currentIndex = ref(props.current)
const scale = ref(1)
const rotation = ref(0)
const isTransitioning = ref(true)

const currentImage = computed(() => props.images[currentIndex.value]?.src || '')
const currentAlt = computed(() => props.images[currentIndex.value]?.alt || '')

watch(() => props.current, (newVal) => {
  currentIndex.value = newVal
  resetTransform()
})

watch(() => props.visible, (newVal) => {
  if (newVal) {
    resetTransform()
  }
})

const resetTransform = () => {
  scale.value = 1
  rotation.value = 0
  isTransitioning.value = true
}

const handleClose = () => {
  emit('update:visible', false)
}

const handlePrev = () => {
  if (currentIndex.value > 0) {
    currentIndex.value--
    emit('update:current', currentIndex.value)
  }
}

const handleNext = () => {
  if (currentIndex.value < props.images.length - 1) {
    currentIndex.value++
    emit('update:current', currentIndex.value)
  }
}

const handleZoomIn = () => {
  isTransitioning.value = true
  scale.value = Math.min(scale.value + 0.25, 5)
}

const handleZoomOut = () => {
  isTransitioning.value = true
  scale.value = Math.max(scale.value - 0.25, 0.25)
}

const handleResetZoom = () => {
  isTransitioning.value = true
  scale.value = 1
  rotation.value = 0
}

const handleRotateLeft = () => {
  isTransitioning.value = true
  rotation.value -= 90
}

const handleRotateRight = () => {
  isTransitioning.value = true
  rotation.value += 90
}

const handleImageLoad = () => {
  // 图片加载完成
}

const handleKeydown = (e: KeyboardEvent) => {
  if (!props.visible) return
  
  switch (e.key) {
    case 'Escape':
      handleClose()
      break
    case 'ArrowLeft':
      handlePrev()
      break
    case 'ArrowRight':
      handleNext()
      break
    case '+':
    case '=':
      handleZoomIn()
      break
    case '-':
    case '_':
      handleZoomOut()
      break
  }
}

onMounted(() => {
  document.addEventListener('keydown', handleKeydown)
})

onUnmounted(() => {
  document.removeEventListener('keydown', handleKeydown)
})
</script>

<style scoped>
.image-preview-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.9);
  z-index: 10000;
  display: flex;
  flex-direction: column;
}

/* 顶部工具栏 */
.preview-header {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 56px;
  background: rgba(0, 0, 0, 0.6);
  backdrop-filter: blur(10px);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 24px;
  z-index: 10001;
}

.header-left {
  display: flex;
  align-items: center;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 8px;
}

.preview-counter {
  color: white;
  font-size: 14px;
  user-select: none;
  font-weight: 500;
}

.toolbar-btn {
  background: transparent;
  border: none;
  color: rgba(255, 255, 255, 0.85);
  cursor: pointer;
  padding: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s;
  border-radius: 4px;
}

.toolbar-btn:hover:not(:disabled) {
  background: rgba(255, 255, 255, 0.1);
  color: white;
}

.toolbar-btn:disabled {
  opacity: 0.3;
  cursor: not-allowed;
}

.toolbar-divider {
  width: 1px;
  height: 20px;
  background: rgba(255, 255, 255, 0.2);
  margin: 0 4px;
}

/* 图片内容区域 */
.preview-content {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  padding: 56px 80px 0;
}

.image-container {
  display: flex;
  align-items: center;
  justify-content: center;
  transform-origin: center center;
}

.preview-image {
  max-width: 100%;
  max-height: calc(100vh - 56px);
  object-fit: contain;
  user-select: none;
  display: block;
}

/* 左右切换按钮 */
.nav-btn {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  background: rgba(0, 0, 0, 0.5);
  backdrop-filter: blur(10px);
  border: none;
  color: white;
  cursor: pointer;
  padding: 16px 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s;
  border-radius: 4px;
  z-index: 10001;
}

.nav-btn:hover:not(:disabled) {
  background: rgba(0, 0, 0, 0.7);
}

.nav-btn:disabled {
  opacity: 0.3;
  cursor: not-allowed;
}

.nav-btn-prev {
  left: 24px;
}

.nav-btn-next {
  right: 24px;
}

/* 动画效果 */
.preview-fade-enter-active,
.preview-fade-leave-active {
  transition: opacity 0.3s ease;
}

.preview-fade-enter-from,
.preview-fade-leave-to {
  opacity: 0;
}

.preview-fade-enter-active .preview-header,
.preview-fade-leave-active .preview-header {
  transition: transform 0.3s ease;
}

.preview-fade-enter-from .preview-header {
  transform: translateY(-100%);
}

.preview-fade-leave-to .preview-header {
  transform: translateY(-100%);
}
</style>
